var express = require('express');
//引入mysql模块
var mysql = require('mysql');
//引入数据库配置模块
var dbconfig = require('./dbconfig');

var router = express.Router();

//数据库配置 [属性名固定，会和mysql模块对应]
// let config = {
//     host:'localhost',
//     port:'3306',
//     user:'root',
//     password:'root',
//     database:'web30database'
// }

//查询年级请求
router.get('/selectStudent', function(req, res, next) {
    //创建数据库连接池
    var pool = mysql.createPool(dbconfig.config);
    //创建数据库连接
    pool.getConnection((err,conn)=>{
        if(err){
            console.log('连接失败！');
            return;
        }
        //定义sql
        let sql = "SELECT gradeId, stuSex, COUNT(1) AS rs FROM student GROUP BY gradeId,stuSex HAVING COUNT(1) > 1";
        //执行SQL业务
        conn.query(sql,(err,data)=>{
            if(err){
                console.log('sql执行失败！');
                return;
            }
            console.log(data);
            res.send({"stuData":data});
        })
        //释放连接
        conn.release();
    })

});

module.exports = router;
